package cn.yuyiling.jelly.sr.controller;

import cn.yuyiling.jelly.sr.api.ReviewScheduleService;
import cn.yuyiling.jelly.sr.mongodb.entity.ReviewSchedule;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/review-schedules")
public class ReviewScheduleController {
    private static final Logger logger = LoggerFactory.getLogger(ReviewScheduleController.class);

    @DubboReference(scope = "remote")
    private ReviewScheduleService reviewScheduleService;

    /**
     * 创建新的复习计划
     */
    @PostMapping("/create")
    public ReviewSchedule createReviewSchedule(@RequestParam String userId,
                                                       @RequestParam String knowledgePointId) {
        logger.info("createReviewSchedule: userId={}, knowledgePointId={}", userId, knowledgePointId);
        return reviewScheduleService.createReviewSchedule(userId, knowledgePointId);
    }

    /**
     * 获取用户的所有复习计划
     */
    @GetMapping("/user/{userId}")
    public List<ReviewSchedule> getReviewSchedulesByUser(@PathVariable String userId) {
        List<ReviewSchedule> schedules = reviewScheduleService.getReviewSchedulesByUser(userId);
        return schedules;
    }

    /**
     * 获取用户待复习的知识点
     */
    @GetMapping("/due/{userId}")
    public List<ReviewSchedule> getDueReviews(@PathVariable String userId) {
        List<ReviewSchedule> dueReviews = reviewScheduleService.getDueReviews(userId, LocalDateTime.now());
        return dueReviews;
    }

    /**
     * 更新复习记录
     */
    @PostMapping("/update/{scheduleId}")
    public ReviewSchedule updateReviewRecord(@PathVariable String scheduleId) {
        ReviewSchedule updatedSchedule = reviewScheduleService.updateReviewRecord(scheduleId, LocalDateTime.now());
        return updatedSchedule;
    }

    /**
     * 获取指定复习次数的间隔天数
     */
    @GetMapping("/interval/{reviewCount}")
    public Integer getEbbinghausIntervalDays(@PathVariable int reviewCount) {
        int intervalDays = reviewScheduleService.getEbbinghausIntervalDays(reviewCount);
        return intervalDays;
    }

    /**
     * 每天凌晨1点执行，检查需要提醒的复习
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void checkDueReviews() {
        List<String> allUserIds = getAllUserIds();
        for (String userId : allUserIds) {
            List<ReviewSchedule> dueReviews = reviewScheduleService.getDueReviews(userId, LocalDateTime.now());
            if (!dueReviews.isEmpty()) {
                sendReviewReminders(userId, dueReviews);
            }
        }
    }

    private List<String> getAllUserIds() {
        return List.of("user1", "user2");
    }

    private void sendReviewReminders(String userId, List<ReviewSchedule> dueReviews) {
        logger.info("Sending review reminders to user {}: {} items", userId, dueReviews.size());
    }
}